package com.platform.controller.platform;


import com.google.common.collect.Lists;
import com.platform.common.annotation.Log;
import com.platform.common.constant.Constants;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.domain.model.LoginUser;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.SecurityUtils;
import com.platform.system.domain.dto.TenantDto;
import com.platform.system.domain.dto.device.TenantAuthAiDto;
import com.platform.system.domain.vo.chip.TenantVo;
import com.platform.system.domain.vo.device.DeviceInfoVo;
import com.platform.system.domain.vo.device.TenantAuthAiVo;
import com.platform.system.logic.TenantAuthAiLogic;
import com.platform.system.service.ISysTenantAuthAiService;
import com.platform.system.service.ISysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/tenantAuthAi/config")
public class TenantAuthAiController {

    @Resource
    ISysTenantAuthAiService sysTenantAuthAiService;

    @Resource
    TenantAuthAiLogic tenantAuthAiLogic;

    @Autowired
    ISysTenantService sysTenantService;

    /**
     * 查询
     */
    @Log(title = "批量查询", businessType = BusinessType.OTHER)
    @GetMapping("/selectTenantAuthAiList")
    public TableDataInfo selectTenantAuthAiList(@Validated TenantAuthAiVo tenantAuthAiVo) {
        return sysTenantAuthAiService.selectTenantAuthAiList(tenantAuthAiVo);
    }

    @Log(title = "查询租户大模型等相关的使用权限", businessType = BusinessType.OTHER)
    @GetMapping("/selectTenantAuthAiByTenantUid")
    public AjaxResult selectTenantAuthAiByTenantUid(@Validated TenantAuthAiVo tenantAuthAiVo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tenantAuthAiVo.setTenantUid(loginUser.getUser().getTenantUid());
        TenantAuthAiDto tenantAuthAiDto = sysTenantAuthAiService.selectTenantAuthAiByTenantUid(tenantAuthAiVo);
        tenantAuthAiLogic.buildTenantAuthAi(tenantAuthAiDto);
        return AjaxResult.success(tenantAuthAiDto);
    }



    /**
     * 新增
     */
    @Log(title = "租户新增配置信息", businessType = BusinessType.INSERT)
    @PostMapping("/insertTenantAuthAi")
    public AjaxResult insertTenantAuthAi(@RequestBody @Validated TenantAuthAiVo tenantAuthAiVo) {
        return sysTenantAuthAiService.insertTenantAuthAi(tenantAuthAiVo);
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTenantAuthAi")
    public AjaxResult updateTenantAuthAi(@RequestBody @Validated TenantAuthAiVo tenantAuthAiVo) {
        return sysTenantAuthAiService.updateTenantAuthAi(tenantAuthAiVo);
    }


    /**
     * 查询
     */
    @Log(title = "查询", businessType = BusinessType.OTHER)
    @GetMapping("/selectTenantAuthAi/{uid}")
    public AjaxResult selectTenantAuthAi(@PathVariable("uid") String uid) {
        TenantAuthAiDto tenantAuthAiDto = sysTenantAuthAiService.selectTenantAuthAi(uid);
        if(tenantAuthAiDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        TenantVo tenantVo = new TenantVo();
        tenantVo.setUid(tenantAuthAiDto.getTenantUid());
        TenantDto tenantDto = sysTenantService.selectTenant(tenantVo);
        tenantAuthAiDto.setTenantDtoList(Lists.newArrayList(tenantDto));
        tenantAuthAiLogic.buildTenantAuthAi(tenantAuthAiDto);
        return AjaxResult.success(tenantAuthAiDto);
    }

    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/deleteTenantAuthAi")
    public AjaxResult deleteTenantAuthAi(@RequestBody @Validated TenantAuthAiVo tenantAuthAiVo) {
        return sysTenantAuthAiService.deleteTenantAuthAi(tenantAuthAiVo);
    }

}
